﻿using ClassData;
using System;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;

namespace Console_Server
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建服务器对象，默认监听本机0.0.0.0，端口12345
            SocketServer server = new SocketServer(12345);

            #region 委托初始化

            //处理从客户端收到的消息
            server.HandleRecMsg = new Action<byte[], SocketConnection, SocketServer>((bytes, connection, server) =>
            {
                string msg = Encoding.UTF8.GetString(bytes);
                Console.WriteLine($"收到消息:{msg}");
            });
            //处理服务器启动后事件
            server.HandleServerStarted = new Action<SocketServer>((server) =>
            {

                Console.WriteLine("服务已启动************");
            });

            //处理新的客户端连接后的事件
            server.HandleNewClientConnected = new Action<SocketServer, SocketConnection>((server, connection) =>
            {
                Console.WriteLine($@"客户端连接，当前连接数为：{server.GetConnectionCount()}");
            });

            //处理客户端连接关闭后的事件
            server.HandleClientClose = new Action<SocketConnection, SocketServer>((connection, server) =>
            {
                Console.WriteLine($@"一个客户端关闭，当前连接数为：{server.GetConnectionCount()}");
            });

            server.HandleResponse = new Action<byte[], SocketServer, SocketConnection>((bytes, socketServer, socketConnection) =>
            {
                Console.WriteLine("广播信息给其他连接----");
            });

            server.HandleResponse = new Action<byte[], SocketServer, SocketConnection>((bytes, SocketServer, SocketConnection) =>
            {
                Console.WriteLine("[广播消息]:" + Encoding.UTF8.GetString(bytes));
            });
            #endregion



            //服务器启动
            server.StartServer();

            while (true)
            {
                //Console.WriteLine("输入:quit，关闭服务器");
                string? op = Console.ReadLine();

                if (op != null)
                {
                    if (op == "quit")
                        break;
                    else
                    {
                        StatusInfo sendData = new StatusInfo();
                        List<float> term = new List<float>(3);
                        string[] num = op.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 1, j = 0; i <= 9; i++)
                        {
                            if (i <= num.Length)
                            {
                                try
                                {
                                    term.Add((float)Convert.ToDouble(num[i - 1]));
                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("[错误] : 输入值有误");
                                }
                            }
                            else
                            {
                                term.Add(0);
                            }
                            if (i % 3 == 0)
                            {
                                if (j == 0)
                                {
                                    sendData.Position = new Vector3(term.ToArray());
                                }
                                else if (j == 1)
                                {
                                    sendData.Direction = new Vector3(term.ToArray());
                                }
                                else
                                {
                                    sendData.Rotation = new Vector3(term.ToArray());
                                }
                                j++;
                                term.Clear();
                            }
                        }

                        string data = JsonSerializer.Serialize(sendData);
                        server.Broadcast(data);
                    }
                }
            }
        }
    }
}
